﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dqs_DAL
{
	public class Seat_DAL
	{
		/// <summary>
		/// 插入座位信息
		/// </summary>
		/// <param name="seats"></param>
		/// <returns></returns>
		public bool InsertSeatInfo(SeatInfo seats)
		{
			using (MyDbContext db = new MyDbContext())
			{
				if (seats == null)
					return false;
				db.SeatInfos.Add(seats);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过Id修改座位信息
		/// </summary>
		/// <param name="seatNew"></param>
		public bool UpdateSeatInfoById(SeatInfo seatNew)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var seatOld = db.SeatInfos.Where(x => x.Id == seatNew.Id).FirstOrDefault();
				if (seatOld == null)
				{
					return false;
				}
				seatOld.StudentID = seatNew.StudentID;
				seatOld.SeatNumber = seatNew.SeatNumber;
				seatOld.IsOccupied = seatNew.IsOccupied;
				seatOld.ClassID = seatNew.ClassID;
				seatOld.ClassInfos = seatNew.ClassInfos;
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过Id删除座位信息
		/// </summary>
		/// <param name="seatId"></param>
		/// <returns></returns>
		public bool DeleteSeatInfoById(int seatId)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var seat = db.SeatInfos.FirstOrDefault(s => s.Id == seatId);
				if (seat == null)
				{
					return false;
				}
				db.SeatInfos.Remove(seat);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 查询所有座位信息
		/// </summary>
		/// <returns></returns>
		public List<SeatInfo> QuerySeatInfo()
		{
			using (MyDbContext db = new MyDbContext())
			{
				var seats = db.SeatInfos.ToList();
				return seats;
			}
		}

	}
}
